package gui;

import gui.houseeditor.HouseModel;
import gui.houseeditor.strategies.HouseDrawStrategy;
import gui.houseeditor.strategies.MinimalisticHouseDrawStrategy.WalkerImage;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.util.HashMap;
import java.util.Map;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.KeyStroke;
import javax.swing.SwingConstants;

public class DialogSceltaPersonaggi extends JDialog {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	JPanel p = new JPanel();
	JPanel p1 = new JPanel();
	ButtonGroup group;
	Map<String, JRadioButton> mappa;
	HouseModel model;
	HouseDrawStrategy strategy;

	public DialogSceltaPersonaggi(MainFrame f, HouseDrawStrategy strategy) {
		super();
		mappa = new HashMap<String, JRadioButton>();
		this.model = f.getHouseModel();
		setTitle("Scelta Personaggi");
		setResizable(false);
		setModal(true);
		this.setLayout(new BorderLayout());
		p.setLayout(new GridBagLayout());
		init();
		initButtons();
		add(p, BorderLayout.CENTER);
		add(p1, BorderLayout.SOUTH);
		p1.setLayout(new FlowLayout(FlowLayout.RIGHT, 10, 10));
		pack();
		this.setLocationRelativeTo(f);
		this.strategy=strategy;
	}

	private void initButtons() {
		JButton ok = new JButton("OK");
		ok.setAction(new OKAction());
		p1.add(ok);
		this.getRootPane().setDefaultButton(ok);
	}

	@SuppressWarnings("serial")
	private class OKAction extends AbstractAction {

		public OKAction() {
			super("OK");
			putValue(Action.ACCELERATOR_KEY,
					KeyStroke.getKeyStroke("control D"));
		}

		@Override
		public void actionPerformed(ActionEvent e) {
			for (JRadioButton button : mappa.values()) {
				if (button.isSelected()) {
					String s = getImage(button);
					if (s.equals("Fungo Reale")) {
						strategy.setWalkerImage(WalkerImage.FUNGO_REALE);
					} else if (s.equals("Stella")) {
						strategy.setWalkerImage(WalkerImage.STELLA);
					} else if (s.equals("Toad")) {
						strategy.setWalkerImage(WalkerImage.TOAD);
					} else if (s.equals("Tarta")) {
						strategy.setWalkerImage(WalkerImage.TARTA);
					} else if (s.equals("Fungo Maligno")) {
						strategy.setWalkerImage(WalkerImage.FUNGO_MALIGNO);
					} else
						strategy.setWalkerImage(WalkerImage.MARIO);
				}
			}
			 dispose();
		}

		private String getImage(JRadioButton button) {
			for (String st : mappa.keySet()) {
				if (mappa.get(st).equals(button)) {
					return st;
				}
			}
			return null;
		}

	}

	private void init() {

		group = new ButtonGroup();

		JLabel nameF = new JLabel("", SwingConstants.CENTER);
		ImageIcon FungoRealeImg = new ImageIcon("img/Fungo Reale.png");
		nameF.setIcon(FungoRealeImg);
		p.add(nameF, new GridBagConstraints(0, 1, 1, 1, 1.0, 1.0,
				GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(
						10, 10, 5, 2), 0, 0));
		JRadioButton FReale = new JRadioButton("Fungo Reale");
		group.add(FReale);
		p.add(FReale, new GridBagConstraints(2, 1, 1, 1, 1.0, 1.0,
				GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(
						10, 0, 5, 10), 0, 0));

		JLabel nameS = new JLabel("", SwingConstants.CENTER);
		ImageIcon StellaImg = new ImageIcon("img/Stella.png");
		nameS.setIcon(StellaImg);
		p.add(nameS, new GridBagConstraints(0, 2, 1, 1, 1.0, 1.0,
				GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(
						10, 10, 5, 2), 0, 0));
		JRadioButton Stella = new JRadioButton("Stella");
		group.add(Stella);
		p.add(Stella, new GridBagConstraints(2, 2, 1, 1, 1.0, 1.0,
				GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(
						10, 0, 5, 10), 0, 0));

		JLabel nameT = new JLabel("", SwingConstants.CENTER);
		ImageIcon TartaImg = new ImageIcon("img/Tarta.png");
		nameT.setIcon(TartaImg);
		p.add(nameT, new GridBagConstraints(3, 1, 1, 1, 1.0, 1.0,
				GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(
						10, 10, 5, 2), 0, 0));
		JRadioButton Tarta = new JRadioButton("Tarta");
		group.add(Tarta);
		p.add(Tarta, new GridBagConstraints(4, 1, 1, 1, 1.0, 1.0,
				GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(
						10, 0, 5, 10), 0, 0));

		JLabel nameTO = new JLabel("", SwingConstants.CENTER);
		ImageIcon ToadImg = new ImageIcon("img/Toad.png");
		nameTO.setIcon(ToadImg);
		p.add(nameTO, new GridBagConstraints(3, 2, 1, 1, 1.0, 1.0,
				GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(
						10, 10, 5, 2), 0, 0));
		JRadioButton Toad = new JRadioButton("Toad");
		group.add(Toad);
		p.add(Toad, new GridBagConstraints(4, 2, 1, 1, 1.0, 1.0,
				GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(
						10, 0, 5, 10), 0, 0));

		JLabel nameFM = new JLabel("", SwingConstants.CENTER);
		ImageIcon FungoMalignoImg = new ImageIcon("img/Fungo-maligno.png");
		nameFM.setIcon(FungoMalignoImg);
		p.add(nameFM, new GridBagConstraints(5, 1, 1, 1, 1.0, 1.0,
				GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(
						10, 10, 5, 2), 0, 0));
		JRadioButton FM = new JRadioButton("Fungo Maligno");
		group.add(FM);
		p.add(FM, new GridBagConstraints(6, 1, 1, 1, 1.0, 1.0,
				GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(
						10, 0, 5, 10), 0, 0));

		JLabel nameSM = new JLabel("", SwingConstants.CENTER);
		ImageIcon SuperMarioImg = new ImageIcon("img/SuperMarioIcona.png");
		nameSM.setIcon(SuperMarioImg);
		p.add(nameSM, new GridBagConstraints(5, 2, 1, 1, 1.0, 1.0,
				GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(
						10, 10, 5, 2), 0, 0));
		JRadioButton SM = new JRadioButton("SuperMario");
		group.add(SM);
		p.add(SM, new GridBagConstraints(6, 2, 1, 1, 1.0, 1.0,
				GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(
						10, 0, 5, 10), 0, 0));

		FReale.setSelected(true);

		mappa.put("Fungo Reale", FReale);
		mappa.put("Stella", Stella);
		mappa.put("Toad", Toad);
		mappa.put("Tarta", Tarta);
		mappa.put("Fungo Maligno", FM);
		mappa.put("Super Mario", SM);

	}

}
